package com.sys.controller;

import com.sys.common.aop.Operation;
import com.sys.common.constant.Constant;
import com.sys.common.util.ResponseDateUtil;
import com.sys.common.util.ResultBuilder;
import com.sys.pojo.Role;
import com.sys.service.RoleService;
import com.sys.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Api(tags = {"角色管理"})
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;

    @PostMapping("/listAllRole")
    @RequiresPermissions("role:list")
    @ApiOperation("获取全部角色")
    public ResponseDateUtil getAllRole(@RequestBody Role role) {
        return ResultBuilder.success(roleService.findListByPage(role));
    }

    @PostMapping("/listAll")
    @ApiOperation("获取全部角色")
    public ResponseDateUtil getAll() {
        return ResultBuilder.success(roleService.listAll());
    }

    @PostMapping("/addRole")
    @RequiresPermissions("role:add")
    @ApiOperation("添加角色")
    @Operation("添加角色")
    public ResponseDateUtil addRole(@RequestBody Role role) {
        if (roleService.add(role) > 0) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }

    @PutMapping("/updRole")
    @RequiresPermissions("role:upd")
    @ApiOperation("更新角色信息")
    @Operation("更新角色信息")
    public ResponseDateUtil updRole(@RequestBody Role role) {
        if (roleService.updateData(role) > 0) {
            return ResultBuilder.success(Constant.UPDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.UPDFAILED);
        }
    }

    @DeleteMapping("/delRole")
    @RequiresPermissions("role:del")
    @ApiOperation("删除角色信息")
    @Operation("删除角色信息")
    public ResponseDateUtil delRole(@RequestParam("id") Integer id) {
        if (roleService.delete(id) > 0) {
            return ResultBuilder.success(Constant.DELSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.DELFAILED);
        }
    }

    @GetMapping("/userRole")
    @ApiOperation("获取用户角色")
    public ResponseDateUtil getUserRole(@RequestParam("uId") Integer uid) {
        return ResultBuilder.success(userRoleService.getAllRole(uid));
    }
}
